/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taskmanager;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
 *
 * @author Pavel
 */
public class TaskManager {
    private List<Task> taskChain;
    
    /**
     * 
     * @param list task list
     */
    public TaskManager(List list){
        taskChain = list;
    }
    
    /**
     *
     * @return Information about done and remaining tasks
     * 
     *
     */
    public StatisticInfo calculateStatistics() {
        StatisticInfo info = new StatisticInfo();
        
        for(TaskInfo it: taskChain)
        {
            if (it.isClosed())
                info.numClosed++;
            /*
            if (it.isDelayed())
                info.numDelayed++;
            if (it.isOverdue())
                info.numOverdue++;
             */
        }
        
        info.numRemaining = taskChain.size() - info.numClosed;
        
        return info;
    }
    
    /**Edits parameters of task if they are different
     * 
     * @param id       Task id
     * @param name      Task name
     * @param desc      Task decription
     * @param con       Task contacts
     * @param date      Task due date
     *
     */
    public void editTask(int id, String name, String desc, String con, Date date) {
        Task task = findById(id);
        if (task == null)
                return;
        if (task.getName() != name)
            task.setName(name);
        if (task.getDescription() != desc)
            task.setDescription(desc);
        if (task.getContacts() != con)
            task.setContacts(con);
        if (task.getDueDate() != date)
            task.setDueDate(date);
        if (validateDate(date))
            task.setClosed(false);
    }
    
    
    /**Set status "done" for the task
     * 
     * @param id       Task id      
     *
     */
    public void closeTask(int id) {
        Task task = findById(id);
        if (task == null)
                return;
        task.setClosed(true);
    }
    
    /**Creates new task
     * 
     * @param name      Task name
     * @param desc      Task decription
     * @param con       Task contacts
     * @param date      Task due date
     * @return  created task
     */
    public TaskInfo createTask(String name, String desc, String con, Date date) {
        Task task = new Task(name, desc, con, date);
        taskChain.add(task);
        return task;
    }
    
    /**Get list of tasks
     * 
     * @return Pointer for list of tasks
     */
    public List<TaskInfo> getAll() {
        return (List)taskChain;
    }
    
    /**Removes specified task
     * 
     * @param id     Task ID
     */
    public void removeTask(int id) {
        Task task = findById(id);
        if (task == null)
                return;
        taskChain.remove(task);
    }
    
    /**Checks if the date is in future
     * 
     * @param date     Specified date for checking
     * @return Result of checking if the date is in future
     */
    public boolean validateDate(Date date) {
        Date now = new Date();
        return date.after(now);
    }
    
    /**Searches for task specified by its ID
     * 
     * @param id    Task ID
     * @return Task specified by ID
     */
    public Task findById(int id){
        for(Task task: taskChain){
            if (task.getId() == id)
                return task;
        }
        return null;
    }
}
